package parser;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Vector;

import net.rim.device.api.xml.parsers.DocumentBuilderFactory;
import net.rim.device.api.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import beans.Course;
import beans.Group;
import beans.GroupFile;
import beans.Message;
import beans.Person;

public class GetGrupoParser {

	public void parseXML(Course c, String xml) {

		try {
			
			Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder()
			.parse(new ByteArrayInputStream(xml.getBytes("ISO-8859-1")));
			doc.getDocumentElement().normalize();
			NodeList list = doc.getElementsByTagName("*");

			String node = new String();
			String element = new String();

			Group group = null;
			Person person = null;
			Message message = null;
			GroupFile file = null;

			for (int i = 0; i < list.getLength(); i++) {
				if (list.item(i).hasChildNodes()){
					Node value = list.item(i).getChildNodes().item(0);
					node = list.item(i).getNodeName();
					element = value.getNodeValue();

					System.out.println("******************************************"+node+": "+element);

					// grupo
					if (node.equals("Grupo")){
						System.out.println("******************************///// Grupo ");
						Element elem = (Element) list.item(i);
						group = parseGroup(elem);
					} else
						if (node.equals("Persona")){
							Element elem = (Element) list.item(i);
							person = parsePerson(elem);
							group.addMember(person);
						} else
							if (node.equals("Mensaje")){ 
								Element elem = (Element) list.item(i);
								message = parseMessage(elem);
								group.addMessage(message);	
							} else
								if(node.equals("ArchivoGrupo")){
									Element elem = (Element) list.item(i);
									file = parseGroupFile(elem);
									group.addFile(file);

								}
				}

			}

			c.setGroup(group);

		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		}

		System.out.println(" --------------- DONE ");
	}


	private Person parsePerson(Element elem) {
		Person pe = new Person();
		String name = ParserMethods.getTextValue(elem, "Nombre");
		pe.setName(name);
		return pe;
	}

	private Message parseMessage(Element elem){
		Message me = new Message();
		String usuario = ParserMethods.getTextValue(elem, "Usuario");
		String fecha = ParserMethods.getTextValue(elem, "Fecha");
		String mensaje = ParserMethods.getTextValue(elem, "Texto");
		me.setUser(usuario);
		me.setDate(fecha);
		me.setMessage(mensaje);
		return me;
	}

	private Group parseGroup(Element elem) {
		Group gr = new Group();
		String id = ParserMethods.getTextValue(elem, "ID");
		String name = ParserMethods.getTextValue(elem, "Nombre");
		gr.setID(id);
		gr.setName(name);
		gr.setMessages(new Vector());
		return gr;
	}


	private GroupFile parseGroupFile(Element elem) {
		return new GroupFile(ParserMethods.getTextValue(elem,"Nombre"),ParserMethods.getTextValue(elem, "Link"),ParserMethods.getTextValue(elem, "Extension"),
				Long.parseLong(ParserMethods.getTextValue(elem, "Size")),ParserMethods.getTextValue(elem, "Fecha"),ParserMethods.getTextValue(elem, "NameUploader"));
	}


}
